package projEscola;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletCadastrarTurma
 */
@WebServlet("/SCTurma")
public class ServletCadastrarTurma extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletCadastrarTurma() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String IdT = request.getParameter("IdTurma");
		String NmT = request.getParameter("NmTurma");
		String IdCrT = request.getParameter("IdCrTurma");
		String IdPfT = request.getParameter("IdPfTurma");
		CCurso newCurso = new CCurso(IdCrT," "," "," ");
		CTurma newTurma = new CTurma(IdT,NmT,IdCrT,IdPfT);
		CProfe newProfe = new CProfe(IdPfT,"");
		CEscola e = (CEscola)getServletContext().getAttribute("escola");
		if(e.pesquisarCurso(newCurso)){
			if (e.pesquisarProfessor(newProfe)){
				if(e.cadastrarTurma(newTurma))
					out.println("Turma Cadastrada com sucesso");
				else
					out.println("Erro ao cadastrar turma");
			}
			else 
				out.println("Professor ainda não cadastrado");
		}
		else   
			out.println("Curso não exite digite outro ");
		out.println("<br>");
		out.println("<a href='Index.html'>Voltar</a>");
	}
}
